<template>
  <div class="add-form">
    <el-dialog :title="titleInfo.text+titleInfo.pageTitle" :visible.sync="dialogFormVisible">
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        style="width:40%; margin-left:10px"
      >
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="formBase.tagName" style="width: 400px">
            <!-- <template slot="prepend">标签名称</template> -->
          </el-input>
        </el-form-item>
        <el-form-item label="学科" prop="subjectID">
            <el-select
            v-model="formBase.subjectID"
            placeholder="请选择"
            filterable
            >
              <el-option
                v-for="item in options"
                :key="item.data"
                :label="item.subjectName"
                :value="item.id"
              ></el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/base/users'
import {list} from '@/api/hmmm/subjects'
export default {
  name: 'TagsAdd',
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    formBase: {
      required: true
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      // 判断
      ruleInline: {
        tagName: [
          { required: true, message: '标签名不能为空', trigger: 'blur' }
        ],
        subjectID: [
          {
            required: true, message: '请选择学科', trigger: 'change'
          }
        ]
      },
      options: [],
        value: ''
    }
  },
  computed: {},
  created () {
    this.gainSub()
  },
  methods: {
    // 弹窗
    dialogFormV() {
      this.dialogFormVisible = true
    },
    dialogFormH() {
      this.dialogFormVisible = false
    },
    // 表单提交
    createData() {
      // console.log(1)
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.dialogFormH()
          let data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            await update(data).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            // console.log(t)
            await add(this.formBase).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    },
    handleClick() {
      alert('button click')
    },
    // 组件更新
    mounted: function() {},
    updated: function() {},
    async gainSub () {
      try {
        const data = await list()
        const res = await list({pagesize: data.data.counts})
        console.log(res)
        this.options = res.data.items
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped>
</style>
